package lazy2.tool.beangenerator.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import lazy2.tool.beangenerator.*;
import lazy2.tool.beangenerator.gui.BGModel.BeanType;


public class BGView extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JPanel              propContainer = new JPanel(new GridLayout(0,2,5,5));
	private JComboBox<DBType>   dbTypes       = new JComboBox<DBType>(new DBType[] {DBType.MSSQLServer, DBType.MySQL});
	private JTextField          dbAddress     = new JTextField();
	private JTextField          dbName        = new JTextField();
	private JTextField          dbUser        = new JTextField();
	private JTextField          dbPassword    = new JPasswordField();
	private JTextField          dbTables      = new JTextField();
	private JTextField          packageName   = new JTextField();
	private JTextField          outputPath    = new JTextField();
	private JComboBox<BeanType> beanTypes     = new JComboBox<BeanType>(new BeanType[] {BeanType.JPA, BeanType.DBXML});
	private JButton             genButton     = new JButton("產生");
	
	private void addPropertyToContainer(String labelName, JComponent...components) {
		if(components!=null) {
			int len = components.length;
			if(len==0) return;
			JLabel label = new JLabel(labelName);
			label.setHorizontalAlignment(JLabel.RIGHT);
			propContainer.add(label);
			if(len==1) {
				propContainer.add(components[0]);
			} else {
				JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
				for(JComponent c:components) {
					panel.add(c);
				}
				propContainer.add(panel);
			}
		}
	}
	public void showErrorMessage(String message) {JOptionPane.showMessageDialog(this, message, "錯誤", JOptionPane.ERROR_MESSAGE);}
	public void showMessage(String message)      {JOptionPane.showMessageDialog(this, message);}
	public void addGenerateBeanListener(ActionListener listener) {genButton.addActionListener(listener);}
	private void initComponents() {
		setLayout(new BorderLayout());
		add(propContainer, BorderLayout.CENTER);
		propContainer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		dbAddress.setColumns(12);
		outputPath.setEditable(false);
		outputPath.setColumns(20);
		final JButton fileBtn = new JButton("選擇");
		final JPanel self = this;
		fileBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						JFileChooser chooser = new JFileChooser();
						chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
						if(chooser.showSaveDialog(self)==JFileChooser.APPROVE_OPTION) {
							outputPath.setText(chooser.getSelectedFile().getAbsolutePath());
						}
					}
				});
			}
		});
		addPropertyToContainer("資料庫類型",  dbTypes);
		addPropertyToContainer("資料庫位址",  dbAddress);
		addPropertyToContainer("資料庫名稱",  dbName);
		addPropertyToContainer("資料表名稱",  dbTables);
		addPropertyToContainer("登入帳號",    dbUser);
		addPropertyToContainer("登入密碼",    dbPassword);
		addPropertyToContainer("package名稱", packageName);
		addPropertyToContainer("輸出位置",    outputPath, fileBtn);
		addPropertyToContainer("產生類型",    beanTypes);
		add(genButton, BorderLayout.SOUTH);
	}
	
	public BGView() {
		initComponents();
	}
	
	public DBType getSelectedDBType() {return (DBType)dbTypes.getSelectedItem();}
	public String getDBAddress()      {return dbAddress.getText();}
	public String getDBName()         {return dbName.getText();}
	public String getDBTables()       {return dbTables.getText();}
	public String getDBUser()         {return dbUser.getText();}
	public String getDBPassword()     {return dbPassword.getText();}
	public String getPackageName()    {return packageName.getText();}
	public String getOutputPath()     {return outputPath.getText();}
	public BeanType getBeanType()     {return (BeanType)beanTypes.getSelectedItem();}
	public void setGenButtonEnabled(boolean enabled) {genButton.setEnabled(enabled);}
}
